import { getProvinceApi } from "@/api/common";
import { getListApi } from "@/api/configCenter/paymentManagement";
import { getAllCompanyApi } from "@/api/managementCenter/departmentPersonnel";
import { reactive, ref } from "vue";

export default function InitHook() {
  const formState = reactive<OutBoundAgent.AddForm>({
    name: "",
    code: "",
    address: "",
    accountId: undefined,
    accountName: "",
    deliveryType: 1,
    provinceCode: "",
    provinceName: "",
    cityCode: "",
    cityName: "",
    areaCode: "",
    areaName: "",
    addressModel: [],
    domainName: "",
    apikey: "",
    bizOutWarehouseBrokerContactList: [
      {
        contact: "",
        phone: "",
        address: "",
        provinceCode: "",
        provinceName: "",
        cityCode: "",
        cityName: "",
        areaCode: "",
        areaName: "",
        addressModel: []
      }
    ],
    companyKeyList: [
      {
        companyName: "",
        companyId: "",
        companyKey: ""
      }
    ]
  });
  const formDataRef = ref<any>(null);
  const loading = ref<boolean>(false);
  const areaOpts = ref<Array<Base.GeneraDataType>>([]);
  const accountOpts = ref<Base.DialogOpts[]>([]);
  const companyOpts = ref<Base.GeneraDataType[]>([]);
  const getAreaOpts = () => {
    return getProvinceApi().then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        areaOpts.value = data;
      }
    });
  };
  const getAccountOpts = () => {
    return getListApi({ pageNum: 1, pageSize: 99999, status: 1 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        accountOpts.value = data.records || [];
      }
    });
  };
  //获取公司
  const getCompanyOpts = () => {
    return getAllCompanyApi().then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        companyOpts.value = data || [];
      }
    });
  };
  return {
    formDataRef,
    formState,
    loading,
    areaOpts,
    getAreaOpts,
    accountOpts,
    getAccountOpts,
    getCompanyOpts,
    companyOpts
  };
}
